package com.company;

import java.util.List;

/**
 * Created by v on 17-7-9.
 */
public class Hero extends Flyer{
    private int life;  //还剩下多少条命
    private boolean doubleFire;  //是否为双倍火力

    public boolean isDoubleFire() {
        return doubleFire;
    }

    public int getLife() {
        return life;
    }

    //增命
    public void addLife(){
        life++;
    }

    //减命
    public void subtractLife(){
        life--;
        doubleFire=false;
    }

    public void setDoubleFire(boolean doubleFire) {
        this.doubleFire = doubleFire;
    }

    public Hero(){
        image=Image.hero0;
        width=image.getWidth();
        height=image.getHeight();
        x=Main.WIDTH/2-width/2;
        y=Main.HEIGHT-height;

        life=3;  //初始三条命
        doubleFire=false;  //初始没有双倍火力
    }

    //英雄类型的标记
    private long index=0;

    @Override
    public void step() {
        if(index%20/2==0){
            image=Image.hero0;
        }else {
            image=Image.hero1;
        }
        index++;
    }

    /**
     * 按照鼠标传来的坐标进行移动
     */
    public void move(int x,int y){
        this.x=x-width/2;
        this.y=y-height/2;
    }

    @Override
    public boolean outOfBound() {
        return x>Main.HEIGHT&&x<0&&y>Main.WIDTH&&y<0;
    }

    //发射子弹
    public void fire(List<Bullet> list){
        if(doubleFire){
            Bullet bullet1=new Bullet(x+width/3,y);
            Bullet bullet2=new Bullet(x+(2*width)/3,y);
            list.add(bullet1);
            list.add(bullet2);
        }else {
            Bullet bullet=new Bullet(x+width/2,y);
            list.add(bullet);
        }
    }

    //检查是否与飞行物发生碰撞
    public boolean hit(Flyer flyer){
        int x1=flyer.x-width/2;
        int x2=flyer.x+width/2+flyer.width;
        int y1=flyer.y-height/2;
        int y2=flyer.y+height/2+flyer.height;

        int herox=x+width/2;
        int heroy=y+herox/2;

        return herox>x1&&herox<x2&&heroy>y1&&heroy<y2;
    }
}
